package com.scm.biz.service._impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.scm.biz.bean.BusinessFile;
import com.scm.biz.bean.SampleStore;
import com.scm.biz.dao.BusinessConstantDao;
import com.scm.biz.dao.BusinessFileDao;
import com.scm.biz.dao.SampleStoreDao;
import com.scm.biz.dto.SampleStoreDto;
import com.scm.biz.enums.FileCategoryEnum;
import com.scm.biz.query.SampleStoreQuery;
import com.scm.biz.result.SampleStoreResult;
import com.scm.biz.service.BusinessConstantService;
import com.scm.biz.service.BusinessFileService;
import com.scm.biz.service.SampleStoreService;
import com.scm.core.bean.exp.UfdmException;
import com.scm.utils.Const;
import com.scm.utils.UploadAndDownUtil;

/**
 * 样衣类业务类的实现类
 * @author Young
 * @Date 2016.11.23
 */
@Service("sampleStoreService")
public class SampleStoreServiceImpl implements SampleStoreService{
	
	@Resource
	private SampleStoreDao sampleStoreDao;
	
	@Resource
	private BusinessFileService businessFileService;
	
	@Resource
	private BusinessConstantService businessConstantService;
	
	@Resource
	private BusinessConstantDao businessConstantDao;
	
	@Resource
	private BusinessFileDao businessFileDao;
	
	@Override
	public void save(SampleStore sampleStore,MultipartHttpServletRequest req) throws Exception {
		String style=req.getParameter("style");
		if(style==null||style.trim().isEmpty()){
			throw new UfdmException("样衣风格为空");
		}
		String[] styles=style.split(Const.SPLIT);
		if(styles.length==0){
			throw new UfdmException("样衣风格为空");
		}
		List<MultipartFile> images=req.getFiles("images");
		if(images.size()>5){
			throw new UfdmException("样衣图片超过5张");
		}
		if(images.size()==0){
			throw new UfdmException("样衣无图片");
		}
		String mainImg=req.getParameter("mainImage");
		if(mainImg==null) mainImg="0";
		int mainImage=new Integer(mainImg);
		String image=UploadAndDownUtil.uploadImage(images.get(mainImage), FileCategoryEnum.SAMPLE_IMAGE.value());
		sampleStore.setCode(Const.getSampleCode());
		sampleStore.setMainImage(image);
		sampleStore.setCreateAt(new Date());
		sampleStoreDao.insert(sampleStore);
		businessConstantService.save(style, sampleStore.getId(), FileCategoryEnum.SAMPLE_STYLE.value());
		businessFileService.saveImage(images, sampleStore.getId(),FileCategoryEnum.SAMPLE_IMAGE.value(),mainImage,image);
	}

	@Override
	public List<SampleStoreDto> getList(SampleStoreQuery query) {
		int total=sampleStoreDao.selectTotal(query);
		query.setRecourdCount(total);
		return sampleStoreDao.selectList(query);
	}

	@Override
	public SampleStoreResult getById(int id) {
		return sampleStoreDao.selectById(id);
	}

	@Override
	public List<SampleStoreResult> selectListByIds(String[] ids) {
		if(ids.length==0){
			return new ArrayList<SampleStoreResult>();
		}
		List<Integer> list=new ArrayList<Integer>();
		for(String id:ids){
			list.add(new Integer(id));
		}
		return sampleStoreDao.selectListByIds(list);
	}

	@Override
	public void modifyById(SampleStore sampleStore, MultipartHttpServletRequest req) throws Exception {
		String style=req.getParameter("style");
		if(style==null||style.trim().isEmpty()){
			throw new UfdmException("样衣风格为空");
		}
		String[] styles=style.split(Const.SPLIT);
		if(styles.length==0){
			throw new UfdmException("样衣风格为空");
		}
		List<MultipartFile> images=req.getFiles("images");
		String[] index=req.getParameterValues("index");
		if(index!=null){
			if(images.size()+index.length>5){
				throw new UfdmException("样衣图片超过5张");
			}
			if(images.size()==0&&index.length==0){
				throw new UfdmException("样衣无图片");
			}
		}else{
			if(images.size()>5){
				throw new UfdmException("样衣图片超过5张");
			}
			if(images.size()==0){
				throw new UfdmException("样衣无图片");
			}
		}
		businessConstantDao.deleteByCustomerId(sampleStore.getId(), FileCategoryEnum.SAMPLE_STYLE.value());
		List<BusinessFile> list=businessFileDao.selectByBusinessId(sampleStore.getId().longValue(), FileCategoryEnum.SAMPLE_IMAGE.value());
		String main=req.getParameter("main");
		if(main!=null){
			int mainImage=new Integer(main);
			String image=list.get(mainImage).getBusinessFile();
			sampleStore.setMainImage(image);
		}
		if(index!=null){
			for(int i=index.length-1;i>=0;i--){
				list.remove(new Integer(index[i]).intValue());
			}
		}
		for(BusinessFile file:list){
			businessFileDao.deleteByPrimaryKey(file.getId());
			UploadAndDownUtil.removeFile(file.getBusinessFile());
		}
		String mainImg=req.getParameter("mainImage");
		int mainImage=-1;
		String image=null;
		if(mainImg!=null){
			mainImage=new Integer(mainImg);
			image=UploadAndDownUtil.uploadImage(images.get(mainImage), FileCategoryEnum.SAMPLE_IMAGE.value());
			sampleStore.setMainImage(image);
		}
		sampleStoreDao.updateByPrimaryKey(sampleStore);
		businessConstantService.save(style, sampleStore.getId(), FileCategoryEnum.SAMPLE_STYLE.value());
		businessFileService.saveImage(images, sampleStore.getId(),FileCategoryEnum.SAMPLE_IMAGE.value(),mainImage,image);
	}
	
}
